﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace Enough.Converter
{
    public class MinStringLengthToBooleanConverter : IValueConverter
    {
        public virtual object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return IsMinimumLengthReached(value, parameter);
        }

        public virtual bool IsMinimumLengthReached(object value, object parameter)
        {
            if (value == null)
            {
                return false;
            }
            int minimumLength;
            if (parameter == null)
            {
                minimumLength = 1;
            }
            else
            {
                if (parameter is int)
                {
                    minimumLength = (int)parameter;
                }
                else
                {
                    bool success = int.TryParse(parameter.ToString(), out minimumLength);
                    if (!success)
                    {
                        minimumLength = 1;
                    }
                }
            }
            return value.ToString().Length >= minimumLength;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
